#!/usr/bin/env bash

# I'm sick and tired of all the churn the three versions of autoconf
# are causing in this repo. Stop committing the configure scripts
# and just autoregen.
./autogen.sh || exit 1

# where are we?
top=$(pwd)
if test -z "${top}" ; then exit 1; fi

# fix
chmod +x vs/sdl/build-scripts/strip_fPIC.sh
chmod +x vs/sdl2/build-dosbox.sh

orig_CFLAGS="${CFLAGS}"
orig_LDFLAGS="${LDFLAGS}"
orig_CPPFLAGS="${CPPFLAGS}"
orig_CXXFLAGS="${CXXFLAGS}"

# Remove our temporary copies of dosbox-x executable before rebuilding
rm -f src/dosbox-x-arm64 src/dosbox-x-x86_64

do_cleanup() {
    rm -rf vs/sdl2/linux-host vs/sdl2/linux-build
    rm -rf vs/zlib/linux-host vs/zlib/linux-build
    rm -rf vs/libpng/linux-host vs/libpng/linux-build
    rm -rf vs/freetype/linux-host vs/freetype/linux-build
    [ -e Makefile ] && make clean
}

universal=0
architectures="$(uname -m)"
#if [ "${1}" = "universal" ]; then
#    shift
#    if [ "${architectures}" = "arm64" ]; then
#        # We can only build universal binaries on an arm64 host because we
#        # need homebrew functional under both architectures.
#        universal=1
#        architectures="arm64 x86_64"
#    fi
#fi

arm64_brew_cmd=""
x86_64_brew_cmd=""
# arm64 native Homebrew
#if [ -x /opt/homebrew/bin/brew ]; then
#    arm64_brew_cmd="/opt/homebrew/bin/brew"
#fi

# x86_64 Homebrew
#if [ -x /usr/local/bin/brew ]; then
#    # old homebrew
#    x86_64_brew_cmd="/usr/local/bin/brew"
#elif [ -x /usr/local/Homebrew/bin/brew ]; then
#    # new homebrew
#    x86_64_brew_cmd="/usr/local/Homebrew/bin/brew"
#fi

# x86_64 on arm64 for universal builds if x86_64 Homebrew is installed
#if [ -n "${x86_64_brew_cmd}" ] && [ "${universal}" -eq 1 ]; then
#    x86_64_brew_cmd="/usr/bin/arch -x86_64 ${x86_64_brew_cmd}"
#fi

for arch in ${architectures}; do
#    declare brew_cmd="${arch}_brew_cmd"
#    if [ -n "${!brew_cmd}" ]; then
#        ${!brew_cmd} list fluid-synth &>/dev/null || ${!brew_cmd} install fluid-synth
#        ${!brew_cmd} list libslirp &>/dev/null || ${!brew_cmd} install libslirp
#        ${!brew_cmd} list pkg-config &>/dev/null || ${!brew_cmd} install pkg-config
#    fi

    do_cleanup

    # arch_flags="-arch ${arch} -mmacosx-version-min=10.11 "
    arch_flags="-arch ${arch} "
    CFLAGS="${arch_flags}${orig_CFLAGS}"
    LDFLAGS="${arch_flags}${orig_LDFLAGS}"
    CPPFLAGS="${arch_flags}${orig_CPPFLAGS}"
    CXXFLAGS="${arch_flags}${orig_CXXFLAGS}"
    export CFLAGS LDFLAGS CPPFLAGS CXXFLAGS

    # prefer to compile against our own copy of SDL 2.x IF the system does not provide one
    
    #echo "Compiling our internal SDL 2.x"
    #(cd vs/sdl2 && ./build-dosbox.sh) || exit 1
    #new="-I${top}/vs/sdl2/linux-host/include "
    #nld="-L${top}/vs/sdl2/linux-host/lib "
    #CFLAGS="${CFLAGS}${new}"
    #LDFLAGS="${LDFLAGS}${nld}"
    #CPPFLAGS="${CPPFLAGS}${new}"
    CXXFLAGS="${CXXFLAGS}${new}"
    export CFLAGS LDFLAGS CPPFLAGS CXXFLAGS

    # prefer to compile against our own copy of SDLnet 1.x
    echo "Compiling our internal SDLnet 1.x"
    (cd vs/sdlnet && ./build-dosbox.sh) || exit 1

    # prefer to compile against our own zlib
    #echo "Compiling our internal zlib"
    #(cd vs/zlib && ./build-dosbox.sh) || exit 1
    #new="-I${top}/vs/zlib/linux-host/include "
    #nld="-L${top}/vs/zlib/linux-host/lib "
    #CFLAGS="${CFLAGS}${new}"
    #LDFLAGS="${LDFLAGS}${nld}"
    #CPPFLAGS="${CPPFLAGS}${new}"
    #CXXFLAGS="${CXXFLAGS}${new}"
    #export CFLAGS LDFLAGS CPPFLAGS CXXFLAGS

    # prefer to compile against our own libpng (comment this out to disable)
    #echo "Compiling our internal libpng"
    #(cd vs/libpng && ./build-dosbox.sh) || exit 1
    #new="-I${top}/vs/libpng/linux-host/include "
    #nld="-L${top}/vs/libpng/linux-host/lib "
    #CFLAGS="${CFLAGS}${new}"
    #LDFLAGS="${LDFLAGS}${nld}"
    #CPPFLAGS="${CPPFLAGS}${new}"
    #CXXFLAGS="${CXXFLAGS}${new}"
    #export CFLAGS LDFLAGS CPPFLAGS CXXFLAGS

    # prefer to compile against our own freetype
    echo "Compiling our internal freetype"
    (cd vs/freetype && ./build-dosbox.sh) || exit 1
    new="-I${top}/vs/freetype/linux-host/include/freetype2 "
    nld="-L${top}/vs/freetype/linux-host/lib -lfreetype "
    CFLAGS="${CFLAGS}${new}"
    LDFLAGS="${LDFLAGS}${nld}"
    CPPFLAGS="${CPPFLAGS}${new}"
    CXXFLAGS="${CXXFLAGS}${new}"
    INTERNAL_FREETYPE=1
    export CFLAGS LDFLAGS CPPFLAGS CXXFLAGS INTERNAL_FREETYPE

    opts=

    # if Brew has installed packages, try to use those too
#    if [ -n "${!brew_cmd}" ]; then
#        echo "Brew is installed, I'm going to use its libraries too"
#        new="-I$(${!brew_cmd} --prefix)/include "
#        nld="-L$(${!brew_cmd} --prefix)/lib "
#        CFLAGS="${CFLAGS}${new}"
#        LDFLAGS="${LDFLAGS}${nld}"
#        CPPFLAGS="${CPPFLAGS}${new}"
#        CXXFLAGS="${CXXFLAGS}${new}"
#        PKG_CONFIG_PATH="${PKG_CONFIG_PATH}:$(${!brew_cmd} --prefix)/lib/pkgconfig"
#        export CFLAGS LDFLAGS CPPFLAGS CXXFLAGS PKG_CONFIG_PATH
#    fi

#    if [ "${universal}" = 1 ]; then
#        opts="${opts} --enable-universal"
#    fi

    # now compile ourself
    echo "Compiling DOSBox-X"
    chmod +x configure
    ./configure --enable-debug=heavy --prefix=/usr --enable-sdl2 "${opts}" "${@}" || exit 1
    make -j3 || exit 1

    cp src/dosbox-x "src/dosbox-x-${arch}"
done
